package cn.example.game.db.bean;

import lombok.experimental.Accessors;
import lombok.Data;
import lombok.AccessLevel;
import lombok.Setter;
import java.util.Date;
import org.artifact.core.db.BaseBean;
import org.artifact.core.lang.IServer;
import java.util.Map;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import java.util.HashMap;
@Data
@Accessors(chain = true)
@SuppressWarnings({"rawtypes","unchecked"})
/** 英雄 */
public abstract class Hero implements BaseBean<Hero> {
	/** 主键 */
	private long id;
	/** 玩家ID */
	private long pid;
	/** 配置ID */
	private int cid;
	/** 等级 */
	private int lv;
	/** 经验 */
	private int exp;
	/** 创建时间 */
	@Setter(value = AccessLevel.PROTECTED)
	private java.util.Date createDate;
	/** 修改时间 */
	@Setter(value = AccessLevel.PROTECTED)
	private java.util.Date modifiedDate;
	
	


	


	@Override
	public Hero deepCopy() {
		Hero hero = null;
		try {
			hero = (Hero) super.clone();
			hero.createDate = (java.util.Date)this.createDate.clone();
			hero.modifiedDate = (java.util.Date)this.modifiedDate.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		onCopy(hero);
		return hero;
	}

	public <T extends Hero> T build(Hero t){
	    if(t == null) return null;
	    BeanUtil.copyProperties(t, this);
        onRead();
        return (T)this;
	}

	public Hero toSave() {
        if(this.id == 0)
            this.id = (long)IServer.me().getContext().getIdFactory().getId();
		Date now = new Date();
		if (createDate==null) {
			createDate = now;
			modifiedDate = now;
		}else {
			modifiedDate = now;
		}
		onWrite();
		return deepCopy();
	}
	


    /**
     * 写数据
     */
    public Object write(){
        Map<Integer, Object> map = new HashMap<>();
        map.put(3355,this.id);
        map.put(110987,this.pid);
        map.put(98494,this.cid);
        map.put(3466,this.lv);
        map.put(100893,this.exp);
        map.put(1368729290,this.createDate);
        map.put(-626009577,this.modifiedDate);
        return map;
    }

    /**
     * 读数据
     */
    public Hero read(Object object){
        Map map = (Map)object;
        this.id = MapUtil.getLong(map ,3355);
        this.pid = MapUtil.getLong(map ,110987);
        this.cid = MapUtil.getInt(map ,98494);
        this.lv = MapUtil.getInt(map ,3466);
        this.exp = MapUtil.getInt(map ,100893);
        this.createDate = MapUtil.getDate(map ,1368729290);
        this.modifiedDate = MapUtil.getDate(map ,-626009577);
        return this;
    }
    /**
     * 克隆之后
     */
    protected void onCopy(Hero hero){

    }

    /**
     * 写数据之前
     */
    protected void onWrite(){
    }

    /**
     * 读数据之后
     */
    protected void onRead(){
    }

	@Override
	public Object pk() {
		return this.id;
	}
}